const webpack = require("webpack");
const { merge } = require("webpack-merge");
const getBaseWebpackConfig = require("./webpack.config.base");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const isEnvProduction = process.env.NODE_ENV === "production";

const webpackConfig = merge(getBaseWebpackConfig(true), {
	entry: {
		app: isEnvProduction ? "./server/index" : "./src/entry-server",
	},
	output: {
		filename: "server.js",
		libraryTarget: "commonjs2", // 打包成commonjs2规范
	},
	target: "node", // 指定node运行环境
	externals: {},
	optimization: { minimize: false, splitChunks: false },
	plugins: [
		new webpack.DefinePlugin({
			// 指定React环境为服务端
			"process.env.REACT_ENV": JSON.stringify("server"),
		}),
		new webpack.IgnorePlugin({
			resourceRegExp: /dev-compiler/,
		}),
		isEnvProduction &&
			new HtmlWebpackPlugin({
				inject: false,
				template: "public/index.html",
				filename: "public/index.html",
				packTime: +new Date(),
				minify: {
					removeComments: true,
					collapseWhitespace: true,
					removeRedundantAttributes: true,
					useShortDoctype: true,
					removeEmptyAttributes: true,
					removeStyleLinkTypeAttributes: true,
					keepClosingSlash: true,
					minifyJS: true,
					minifyCSS: true,
					minifyURLs: true,
				},
			}),
		isEnvProduction && new CopyPlugin([{ from: "public", to: "public" }, { from: "pm2.prod.config.js" }, { from: "pm2.test.config.js" }]),
	].filter(Boolean),
});
module.exports = webpackConfig;
